<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/1/26
 * Time: 16:11
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Goodscate extends Base
{
    /**
     * 产品分类首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $cate_status = input('cate_status');
            $cate_recommend = input('cate_recommend');
            $where['cate_level'] = 1;
            $where['cate_status'] = ['BETWEEN', [1, 2]];

            if($cate_status) $where['cate_status'] = $cate_status;
            if($cate_recommend) $where['cate_recommend'] = $cate_recommend;

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_goods_cate')->where($where)->count();// 获取总条数
            $lists = Db::table('up_goods_cate')
                ->where($where)
                ->field('cate_pid, cate_level, cate_update_time', true)
                ->page($Nowpage, $limits)
                ->order('cate_sort desc, cate_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 添加一级分类
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function firstGoodsCateAdd(){
        if(Request::instance()->isPost()){
            $name_en = input('name_en');
            $name_es = input('name_es');
            $name_tp = input('name_tp');
            $cate_image = input('cate_image');
            $cate_status = input('cate_status');
            $cate_recommend = input('cate_recommend');
            $cate_sort = input('cate_sort');

            $has = Db::table('up_goods_cate')
                ->where(['cate_name_en' => $name_en, 'cate_level' => 1])
                ->find();
            if($has) return json(['code' => 100, 'msg' => '该分类已存在']);

            $insert = [
                'cate_name_en' => $name_en,
                'cate_name_es' => $name_es,
                'cate_name_tp' => $name_tp,
                'cate_image' => $cate_image,
                'cate_status' => $cate_status,
                'cate_recommend' => $cate_recommend,
                'cate_sort' => $cate_sort,
                'cate_time' => initTime()
            ];
            $res = Db::table('up_goods_cate')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /**
     * 编辑一级分类
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function firstGoodsCateEdit(){
        $cate_id = input('cate_id');
        if(Request::instance()->isPost()){
            $name_en = input('name_en');
            $name_es = input('name_es');
            $name_tp = input('name_tp');
            $cate_image = input('cate_image');
            $cate_status = input('cate_status');
            $cate_recommend = input('cate_recommend');
            $cate_sort = input('cate_sort');

            $has = Db::table('up_goods_cate')
                ->where(['cate_name_en' => $name_en, 'cate_level' => 1])
                ->find();
            if($has && ($cate_id != $has['cate_id'])) return json(['code' => 100, 'msg' => '该分类已存在']);

            $update = [
                'cate_name_en' => $name_en,
                'cate_name_es' => $name_es,
                'cate_name_tp' => $name_tp,
                'cate_status' => $cate_status,
                'cate_recommend' => $cate_recommend,
                'cate_sort' => $cate_sort,
                'cate_update_time' => initTime()
            ];
            if($cate_image) $update['cate_image'] = $cate_image;
            $res = Db::table('up_goods_cate')->where('cate_id', $cate_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $data = Db::table('up_goods_cate')->where('cate_id', $cate_id)->field('cate_level, cate_pid, cate_time, cate_update_time', true)->find();
        return $this->fetch('', ['data' => $data]);
    }

    /**
     * 分类上架/下架
     * @return \think\response\Json
     */
    public function goodsCateChangeStatus(){
        $cate_id = input('id');
        $status = Db::table('up_goods_cate')->where('cate_id', $cate_id)->value('cate_status');
        $newStatus = $status == 1 ? 2 : 1;
        $res = Db::table('up_goods_cate')->where('cate_id', $cate_id)->setField('cate_status', $newStatus);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 一级分类推荐/取消推荐
     * @return \think\response\Json
     */
    public function firstGoodsCateSetRecommend(){
        $cate_id = input('id');
        $recommend = Db::table('up_goods_cate')->where('cate_id', $cate_id)->value('cate_recommend');
        $newRecommend = $recommend == 1 ? 2 : 1;
        $res = Db::table('up_goods_cate')->where('cate_id', $cate_id)->setField('cate_recommend', $newRecommend);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 删除分类
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function goodsCateDel(){
        $cate_id = input('id');
        $res = Db::table('up_goods_cate')->where('cate_id', $cate_id)->setField('cate_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 二级分类列表
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function secondGoodsCate(){
        if(request()->isAjax ()){
            $cate_status = input('cate_status');
            $cate_recommend = input('cate_recommend');
            $where['cate_pid'] = session('f_cate_id');
            $where['cate_status'] = ['BETWEEN', [1, 2]];

            if($cate_status) $where['cate_status'] = $cate_status;
            if($cate_recommend) $where['cate_recommend'] = $cate_recommend;

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_goods_cate')->where($where)->count();// 获取总条数
            $lists = Db::table('up_goods_cate')
                ->where($where)
                ->field('cate_level, cate_pid, cate_image, cate_update_time, cate_recommend', true)
                ->page($Nowpage, $limits)
                ->order('cate_sort desc, cate_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $cate_id = input('cate_id');
        if($cate_id){
            session('f_cate_id', $cate_id);
        }
        return $this->fetch();
    }

    /**
     * 添加二级分类
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function secondGoodsCateAdd(){
        if(Request::instance()->isPost()){
            $name_en = input('name_en');
            $name_es = input('name_es');
            $name_tp = input('name_tp');
            $cate_status = input('cate_status');
            $cate_sort = input('cate_sort');

            $has = Db::table('up_goods_cate')
                ->where(['cate_name_en' => $name_en, 'cate_level' => 2])
                ->find();
            if($has) return json(['code' => 100, 'msg' => '该分类已存在']);

            $insert = [
                'cate_name_en' => $name_en,
                'cate_name_es' => $name_es,
                'cate_name_tp' => $name_tp,
                'cate_level' => 2,
                'cate_pid' => session('f_cate_id'),
                'cate_status' => $cate_status,
                'cate_sort' => $cate_sort,
                'cate_time' => initTime()
            ];
            $res = Db::table('up_goods_cate')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /**
     * 二级分类编辑
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function secondGoodsCateEdit(){
        $cate_id = input('cate_id');
        if(Request::instance()->isPost()){
            $name_en = input('name_en');
            $name_es = input('name_es');
            $name_tp = input('name_tp');
            $cate_status = input('cate_status');
            $cate_sort = input('cate_sort');

            $has = Db::table('up_goods_cate')
                ->where(['cate_name_en' => $name_en, 'cate_level' => 2])
                ->find();
            if($has && ($cate_id != $has['cate_id'])) return json(['code' => 100, 'msg' => '该分类已存在']);

            $update = [
                'cate_name_en' => $name_en,
                'cate_name_es' => $name_es,
                'cate_name_tp' => $name_tp,
                'cate_status' => $cate_status,
                'cate_sort' => $cate_sort,
                'cate_update_time' => initTime()
            ];
            $res = Db::table('up_goods_cate')->where('cate_id', $cate_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $data = Db::table('up_goods_cate')->where('cate_id', $cate_id)->field('cate_level, cate_pid, cate_time, cate_update_time', true)->find();
        return $this->fetch('', ['data' => $data]);
    }

    /**
     * 三级分类列表
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function thirdGoodsCate(){
        if(request()->isAjax ()){
            $cate_status = input('cate_status');
            $where['cate_pid'] = session('s_cate_id');
            $where['cate_status'] = ['BETWEEN', [1, 2]];

            if($cate_status) $where['cate_status'] = $cate_status;

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_goods_cate')->where($where)->count();// 获取总条数
            $lists = Db::table('up_goods_cate')
                ->where($where)
                ->field('cate_level, cate_pid, cate_image, cate_update_time, cate_recommend', true)
                ->page($Nowpage, $limits)
                ->order('cate_sort desc, cate_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $cate_id = input('cate_id');
        if($cate_id){
            session('s_cate_id', $cate_id);
        }
        return $this->fetch();
    }

    /**
     * 添加三级分类
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function thirdGoodsCateAdd(){
        if(Request::instance()->isPost()){
            $name_en = input('name_en');
            $name_es = input('name_es');
            $name_tp = input('name_tp');
            $cate_status = input('cate_status');
            $cate_sort = input('cate_sort');

            $has = Db::table('up_goods_cate')
                ->where(['cate_name_en' => $name_en, 'cate_level' => 3])
                ->find();
            if($has) return json(['code' => 100, 'msg' => '该分类已存在']);

            $insert = [
                'cate_name_en' => $name_en,
                'cate_name_es' => $name_es,
                'cate_name_tp' => $name_tp,
                'cate_level' => 3,
                'cate_pid' => session('s_cate_id'),
                'cate_status' => $cate_status,
                'cate_sort' => $cate_sort,
                'cate_time' => initTime()
            ];
            $res = Db::table('up_goods_cate')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /**
     * 编辑三级分类
     * @return mixed|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function thirdGoodsCateEdit(){
        $cate_id = input('cate_id');
        if(Request::instance()->isPost()){
            $name_en = input('name_en');
            $name_es = input('name_es');
            $name_tp = input('name_tp');
            $cate_status = input('cate_status');
            $cate_sort = input('cate_sort');

            $has = Db::table('up_goods_cate')
                ->where(['cate_name_en' => $name_en, 'cate_level' => 3])
                ->find();
            if($has && ($cate_id != $has['cate_id'])) return json(['code' => 100, 'msg' => '该分类已存在']);

            $update = [
                'cate_name_en' => $name_en,
                'cate_name_es' => $name_es,
                'cate_name_tp' => $name_tp,
                'cate_status' => $cate_status,
                'cate_sort' => $cate_sort,
                'cate_update_time' => initTime()
            ];
            $res = Db::table('up_goods_cate')->where('cate_id', $cate_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $data = Db::table('up_goods_cate')->where('cate_id', $cate_id)->field('cate_id, cate_name_en, cate_name_es, cate_name_tp, cate_status, cate_sort')->find();
        return $this->fetch('', ['data' => $data]);
    }
}